package com.wang.orders.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wang.common.util.PageInfoUtil;
import com.wang.orders.dao.IACartDao;
import com.wang.orders.dao.IAOrdersDao;
import com.wang.orders.dao.IAOrdersGoodsDao;
import com.wang.orders.dao.IAOrdersHistoryDao;
import com.wang.orders.pojo.ACart;
import com.wang.orders.pojo.AOrders;
import com.wang.orders.pojo.AOrdersGoods;
import com.wang.orders.pojo.AOrdersHistory;
import com.wang.orders.service.IOrdersService;
import com.wang.users.dao.IAUsersDao;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IUsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一个模块一个Service
 */
@Service("ordersService")
public class OrdersServiceImpl implements IOrdersService
{
    @Resource
    private IACartDao cartDao;
    @Resource
    private IAOrdersDao ordersDao ;
    @Resource
    private IAOrdersGoodsDao ordersGoodsDao ; 
    @Resource
    private IAOrdersHistoryDao ordersHistoryDao ; 

    /**
     * {
     *      "code":"响应码",
     *      "info":"响应信息",
     *      "data":
     *       {
     *      	"id:":"",
     *      	"effect":""
     *       }
     *  }
     * @param cart   朝代
     * @return
     */
    @Override
    public JSONObject saveOneCartService(ACart cart)
    {
        JSONObject resultJSON = new JSONObject();
        cart.setCreateTime(new Date());
        cart.setUpdateTime(new Date());

        /**
         * 如果当前用户首次添加商品,数量是1;
         * 如果当前用户第二次还是添加相同的商品,那么数量+1
         */
        PageInfoUtil pageInfoUtil = new PageInfoUtil();
        Map<String,Object> extMap = new HashMap<>();
        /* 根据当前用户Id和商品id查询购物车 */
        extMap.put("usersId",cart.getUsersId());
        extMap.put("goodsId",cart.getGoodsId());
        List<ACart> cartList = this.selectCondListCartService(pageInfoUtil, extMap);
        if(pageInfoUtil.getTotalRecord() >= 1 )
        {
            /* 说明查询到了 */
            ACart cartRes = cartList.get(0);
            /* 要更新 */
            cartRes.setNum(cartRes.getNum() + cart.getNum());
            JSONObject resultDbJSON = this.updateOneCartService(cartRes);
            return resultDbJSON ;
        }

        int res = this.cartDao.saveOne(cart);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",cart.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject updateOneCartService(ACart cart)
    {
        JSONObject resultJSON = new JSONObject();

        cart.setUpdateTime(new Date());
        int res = this.cartDao.updateOne(cart);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",cart.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject deleteBatchCartService(Map<String, Object> condMap)
    {
        JSONObject resultJSON = new JSONObject();
        int res = this.cartDao.deleteBatch(condMap);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public ACart selectOneCartService(Map<String, Object> condMap)
    {
        return this.cartDao.selectOne(condMap);
    }

    @Override
    public List<ACart> selectCondListCartService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        /* 查询之前,先解决keyword加%的问题 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        if (pageInfoUtil != null)
        {
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            List<ACart> list = this.cartDao.selectCondList(condMap);
            /* 设置总条数 */
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 返回结果 */
            return list ;
        }
        return this.cartDao.selectCondList(condMap);
    }

    /**
     * {
     *      "code":"响应码",
     *      "info":"响应信息",
     *      "data":
     *       {
     *      	"id:":"",
     *      	"effect":""
     *       }
     *  }
     * @param orders   朝代
     * @return
     */
    @Override
    public JSONObject saveOneOrdersService(AOrders orders)
    {
        JSONObject resultJSON = new JSONObject();
        Map<String,Object> condMap = new HashMap<>();


        orders.setCreateTime(new Date());
        orders.setUpdateTime(new Date());
        orders.setPubTime(new Date());

        int res = this.ordersDao.saveOne(orders);

        if(res > 0 )
        {
            /*
            思路
            保存订单
            保存订单商品明细(从购物车表中获取)
            保存订单历史
            清空当前用户的购物车
            *  */
            /* 根据用户id查询cart */
            condMap.clear();
            condMap.put("usersId",orders.getUsersId());
            List<ACart> cartList = this.selectCondListCartService(null, condMap);

            double totalPrice = 0 ;
            for (ACart cart : cartList)
            {
                /* 循环购物车 */
                AOrdersGoods ordersGoods = new AOrdersGoods() ;

                ordersGoods.setOrdersId(orders.getId());

                ordersGoods.setName(cart.getGoods().getName());
                ordersGoods.setPrice(cart.getGoods().getPrice());
                ordersGoods.setNum(cart.getNum());
                ordersGoods.setTotalPrice(ordersGoods.getNum() * ordersGoods.getPrice());

                /* 保存 */
                this.saveOneOrdersGoodsService(ordersGoods);

                /* 删除购物车 */
                condMap.clear();
                condMap.put("id",cart.getId()) ;
                this.deleteBatchCartService(condMap);

                totalPrice = totalPrice + ordersGoods.getTotalPrice() ;
            }

            orders.setTotalPrice(totalPrice);
            orders.setDealAmount(totalPrice);

            /* 保存订单历史 */
            AOrdersHistory ordersHistory = new AOrdersHistory();
            ordersHistory.setOrdersId(orders.getId());
            ordersHistory.setUsersId(orders.getUsersId());
            ordersHistory.setStatus(orders.getStatus());
            ordersHistory.setContent("下单");
            this.saveOneOrdersHistoryService(ordersHistory);

            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",orders.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject updateOneOrdersService(AOrders orders)
    {
        JSONObject resultJSON = new JSONObject();

        orders.setUpdateTime(new Date());
        int res = this.ordersDao.updateOne(orders);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",orders.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject deleteBatchOrdersService(Map<String, Object> condMap)
    {
        JSONObject resultJSON = new JSONObject();
        int res = this.ordersDao.deleteBatch(condMap);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public AOrders selectOneOrdersService(Map<String, Object> condMap)
    {
        return this.ordersDao.selectOne(condMap);
    }

    @Override
    public List<AOrders> selectCondListOrdersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        /* 查询之前,先解决keyword加%的问题 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        if (pageInfoUtil != null)
        {
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            List<AOrders> list = this.ordersDao.selectCondList(condMap);
            /* 设置总条数 */
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 返回结果 */
            return list ;
        }
        return this.ordersDao.selectCondList(condMap);
    }

    /**
     * {
     *      "code":"响应码",
     *      "info":"响应信息",
     *      "data":
     *       {
     *      	"id:":"",
     *      	"effect":""
     *       }
     *  }
     * @param ordersGoods   朝代
     * @return
     */
    @Override
    public JSONObject saveOneOrdersGoodsService(AOrdersGoods ordersGoods)
    {
        JSONObject resultJSON = new JSONObject();
        ordersGoods.setCreateTime(new Date());
        ordersGoods.setUpdateTime(new Date());
        ordersGoods.setPubTime(new Date());

        int res = this.ordersGoodsDao.saveOne(ordersGoods);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",ordersGoods.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject updateOneOrdersGoodsService(AOrdersGoods ordersGoods)
    {
        JSONObject resultJSON = new JSONObject();

        ordersGoods.setUpdateTime(new Date());
        int res = this.ordersGoodsDao.updateOne(ordersGoods);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",ordersGoods.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject deleteBatchOrdersGoodsService(Map<String, Object> condMap)
    {
        JSONObject resultJSON = new JSONObject();
        int res = this.ordersGoodsDao.deleteBatch(condMap);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public AOrdersGoods selectOneOrdersGoodsService(Map<String, Object> condMap)
    {
        return this.ordersGoodsDao.selectOne(condMap);
    }

    @Override
    public List<AOrdersGoods> selectCondListOrdersGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        /* 查询之前,先解决keyword加%的问题 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        if (pageInfoUtil != null)
        {
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            List<AOrdersGoods> list = this.ordersGoodsDao.selectCondList(condMap);
            /* 设置总条数 */
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 返回结果 */
            return list ;
        }
        return this.ordersGoodsDao.selectCondList(condMap);
    }

    /**
     * {
     *      "code":"响应码",
     *      "info":"响应信息",
     *      "data":
     *       {
     *      	"id:":"",
     *      	"effect":""
     *       }
     *  }
     * @param ordersHistory   朝代
     * @return
     */
    @Override
    public JSONObject saveOneOrdersHistoryService(AOrdersHistory ordersHistory)
    {
        JSONObject resultJSON = new JSONObject();
        ordersHistory.setCreateTime(new Date());
        ordersHistory.setUpdateTime(new Date());
        ordersHistory.setPubTime(new Date());

        int res = this.ordersHistoryDao.saveOne(ordersHistory);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",ordersHistory.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject updateOneOrdersHistoryService(AOrdersHistory ordersHistory)
    {
        JSONObject resultJSON = new JSONObject();

        ordersHistory.setUpdateTime(new Date());
        int res = this.ordersHistoryDao.updateOne(ordersHistory);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("id",ordersHistory.getId());
            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public JSONObject deleteBatchOrdersHistoryService(Map<String, Object> condMap)
    {
        JSONObject resultJSON = new JSONObject();
        int res = this.ordersHistoryDao.deleteBatch(condMap);

        if(res > 0 )
        {
            /* 此条sql执行对数据库影响是 1条,说明插入成功*/
            resultJSON.put("code","0");
            resultJSON.put("info","成功");
            /* data中放数据了 */
            JSONObject dataJSON = new JSONObject();

            dataJSON.put("effect",res);

            resultJSON.put("data",dataJSON);
        }else
        {
            resultJSON.put("code","1");
            resultJSON.put("info","失败");
        }
        return resultJSON;
    }

    @Override
    public AOrdersHistory selectOneOrdersHistoryService(Map<String, Object> condMap)
    {
        return this.ordersHistoryDao.selectOne(condMap);
    }

    @Override
    public List<AOrdersHistory> selectCondListOrdersHistoryService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        /* 查询之前,先解决keyword加%的问题 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        if (pageInfoUtil != null)
        {
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            List<AOrdersHistory> list = this.ordersHistoryDao.selectCondList(condMap);
            /* 设置总条数 */
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 返回结果 */
            return list ;
        }
        return this.ordersHistoryDao.selectCondList(condMap);
    }
}
